#!/bin/bash
source ~/.bashrc
source ~/SHELL/isFileEmpty.sh
if [ $# -ne 4 ]; then
	echo "This shell generate the local wig file to show the local coverage"
	echo "from a large wig file in a given region. "
	echo "The 1st ptr is global wig file path"
	echo "The 2nd ptr is the range start"
	echo "The 3rd ptr is the range end"
	echo "The 4th ptr is the output BED path"
	echo "4 ptrs are needed"
	exit 0
fi
isFileEmpty $1
if (($? > 0)); then
	echo "$1 is not found or empty"
	exit 0
fi
chrName=$(basename $1 .wig)
#echo $chrName
localWigFileName=$chrName"_$2_$3.wig"
outputBedFilePath="$4"
#echo "Try to generate $localWigFileName"
python ~/SCRIPT/one_contig_one_oceans_file.py $1 $localWigFileName $2 $3 0 
if [ ! -s  $localWigFileName ]; then
	echo "fail to generate $localWigFileName"
	exit 0
fi
#OUT_DIR="UCSC"
HTML_DIR=$(basename $1 ".wig")
outputBedFileName=$(basename $outputBedFilePath) 
{
	echo -n "#http://genome.ucsc.edu/cgi-bin/hgTracks?org"
	echo -n "=human&position=$chrName:$2-$3&hgt.customText"
	echo "=http://dw408k-2.cmb.usc.edu:8080/$HTML_DIR/$outputBedFileName" 
	echo "browser position $chrName:$2-$3"
	echo "track type=bedGraph name=\"Coverage\""\
	"description=\"BedGraph format\" visibility=full color=200,100,0"\
	"altColor=0,100,200 priority=20"
}>$outputBedFilePath
cat $localWigFileName >> $outputBedFilePath
\rm $localWigFileName 
#echo "http://genome.ucsc.edu/cgi-bin/hgTracks?org=human&position=$chrName:$2-$3&hgt.customText=http://dw408k-2.cmb.usc.edu:8080/ucsc/$(basename $outputBedFilePath)" 
if [ ! -f  $outputBedFilePath ]; then
	echo "fail to generate $outputBedFilePath"	
fi
exit
